﻿Public Class admin
    Private NhanVienBS As NhanVienBS
    Private NhanVienGD As NhanVienGD
    Private cm As CurrencyManager

    Private Sub admin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        LoadDGV_NhanVien()
        'dgrvNhanVien.Columns(0).HeaderText = "Number"
        dgrvNhanVien.Columns(0).HeaderText = "Mã NV"
        dgrvNhanVien.Columns(1).HeaderText = "Tên NV"
        dgrvNhanVien.Columns(2).HeaderText = "Ngày Sinh"
        dgrvNhanVien.Columns(3).HeaderText = "Địa Chỉ"
        dgrvNhanVien.Columns(4).HeaderText = "Giới Tính"
        dgrvNhanVien.Columns(5).HeaderText = "Điện Thoại"
        dgrvNhanVien.Columns(6).HeaderText = "Trình Độ"

        Load_ComboboxNV()
        Load_ComboboxTrinhDo()
        'dgrvNhanVien.ClearSelection()
        'Dim NhanVienList As New List(Of NhanVienGD)
        'NhanVienList = NhanVienBS.GetData()
        'cm = Me.BindingContext(NhanVienList)
        'txtmanv.DataBindings.Add("Text", NhanVienList, "MaNV")
        'txttennv.DataBindings.Add("Text", NhanVienList, "HoTen")
        'txtngaysinhnv.DataBindings.Add("Text", NhanVienList, "NamSinh")
        'txtdiachinv.DataBindings.Add("Text", NhanVienList, "DiaChi")
        'cbbgioitinhnv.DataBindings.Add("Text", NhanVienList, "GioiTinh")
        'txtdienthoainv.DataBindings.Add("Text", NhanVienList, "DienThoai")
        'cbbtrinhdonv.DataBindings.Add("Text", NhanVienList, "TrinhDo")
        'dgrvNhanVien.DataSource = NhanVienList

        dgrvNhanVien.SelectionMode = DataGridViewSelectionMode.FullRowSelect
    End Sub

    '------------------- Load

    Private Sub LoadDGV_NhanVien()
        dgrvNhanVien.ClearSelection()
        Dim NhanVienList As New List(Of NhanVienGD)
        NhanVienBS = New NhanVienBS
        NhanVienList = NhanVienBS.GetData()
        dgrvNhanVien.DataSource = NhanVienList


    End Sub

    Private Sub Load_ComboboxNV()
        With cbbgioitinhnv
            .Items.Add("Nam")
            .Items.Add("Nữ")

        End With
    End Sub

    Private Sub Load_ComboboxTrinhDo()
        With cbbtrinhdonv
            .Items.Add("Trung Học Phổ Thông")
            .Items.Add("Trung Cấp")
            .Items.Add("Cao Đẳng")
            .Items.Add("Đại Học")
        End With
    End Sub

    Private Sub ClearTextBox()
        txttennv.Text = ""
        txtdiachinv.Text = ""
        txtdienthoainv.Text = ""
    End Sub

    Private Sub ClearBinding()
        txtmanv.DataBindings.Clear()
        txttennv.DataBindings.Clear()
        txtngaysinhnv.DataBindings.Clear()
        txtdiachinv.DataBindings.Clear()
        cbbgioitinhnv.DataBindings.Clear()
        txtdienthoainv.DataBindings.Clear()
        cbbtrinhdonv.DataBindings.Clear()
    End Sub


    '-------------------- Các Button'

    Private Sub btnaddnv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnaddnv.Click

        Dim maNV As String = txtmanv.Text
        Dim hoTen As String = txttennv.Text
        Dim namSinh As Date = txtngaysinhnv.Text
        Dim diaChi As String = txtdiachinv.Text
        Dim gioiTinh As String = cbbgioitinhnv.Text
        Dim dienThoai As String = txtdienthoainv.Text
        Dim trinhDo As String = cbbtrinhdonv.Text
        NhanVienGD = New NhanVienGD(maNV, hoTen, namSinh, diaChi, gioiTinh, dienThoai, trinhDo)
        NhanVienBS.Insert(NhanVienGD)
        Me.GroupBox2.Visible = False
        Me.GroupBox1.Visible = True
        LoadDGV_NhanVien()
        btncapnhatnv.Visible = True
        btnaddnv.Visible = True
    End Sub

    Private Sub btnbacknv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnbacknv.Click
        ClearBinding()
        LoadDGV_NhanVien()
        Me.GroupBox1.Visible = True
        Me.GroupBox2.Visible = False
        btncapnhatnv.Visible = True
        btnaddnv.Visible = True
    End Sub

    Private Sub btnthemnv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthemnv.Click

        Me.GroupBox1.Visible = False
        Me.GroupBox2.Visible = True
        txtmanv.Text = NhanVienBS.GetMaCuoi()
        btncapnhatnv.Visible = False
        ClearTextBox()
        ClearBinding()
        txtmanv.Enabled = False

        
    End Sub

    Private Sub btnxoanv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnxoanv.Click
        Dim maNV As String
        maNV = dgrvNhanVien.SelectedRows.Item(0).Cells(0).Value
        Dim hoTen As String
        hoTen = dgrvNhanVien.SelectedRows.Item(0).Cells(1).Value
        Dim namSinh As Date
        namSinh = dgrvNhanVien.SelectedRows.Item(0).Cells(2).Value
        Dim diaChi As String
        diaChi = dgrvNhanVien.SelectedRows.Item(0).Cells(3).Value
        Dim gioiTinh As String
        gioiTinh = dgrvNhanVien.SelectedRows.Item(0).Cells(4).Value
        Dim dienThoai As String
        dienThoai = dgrvNhanVien.SelectedRows.Item(0).Cells(5).Value
        Dim trinhDo As String
        trinhDo = dgrvNhanVien.SelectedRows.Item(0).Cells(6).Value
        NhanVienGD = New NhanVienGD(maNV, hoTen, namSinh, diaChi, gioiTinh, dienThoai, trinhDo)
        NhanVienBS.Delete(NhanVienGD)
        Me.GroupBox2.Visible = False
        Me.GroupBox1.Visible = True
        LoadDGV_NhanVien()
    End Sub
    

    Private Sub btncapnhatnv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncapnhatnv.Click
        Dim maNV As String = txtmanv.Text
        Dim hoTen As String = txttennv.Text
        Dim namSinh As Date = txtngaysinhnv.Text
        Dim diaChi As String = txtdiachinv.Text
        Dim gioiTinh As String = cbbgioitinhnv.Text
        Dim dienThoai As String = txtdienthoainv.Text
        Dim trinhDo As String = cbbtrinhdonv.Text
        NhanVienGD = New NhanVienGD(maNV, hoTen, namSinh, diaChi, gioiTinh, dienThoai, trinhDo)
        NhanVienBS.Update(NhanVienGD)
        Me.GroupBox2.Visible = False
        Me.GroupBox1.Visible = True
        LoadDGV_NhanVien()
        btncapnhatnv.Visible = True
        btnaddnv.Visible = True

    End Sub

    Private Sub btnsuanv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsuanv.Click
        Me.GroupBox1.Visible = False
        Me.GroupBox2.Visible = True
        txtmanv.Enabled = False
        btnaddnv.Visible = False

        'Test
        Dim maNV As String
        maNV = dgrvNhanVien.SelectedRows.Item(0).Cells(0).Value
        Dim tenNV As String
        tenNV = dgrvNhanVien.SelectedRows.Item(0).Cells(1).Value
        Dim namSinh As Date
        namSinh = dgrvNhanVien.SelectedRows.Item(0).Cells(2).Value
        Dim diaChi As String
        diaChi = dgrvNhanVien.SelectedRows.Item(0).Cells(3).Value
        Dim gioiTinh As String
        gioiTinh = dgrvNhanVien.SelectedRows.Item(0).Cells(4).Value
        Dim dienThoai As String
        dienThoai = dgrvNhanVien.SelectedRows.Item(0).Cells(5).Value
        Dim trinhDo As String
        trinhDo = dgrvNhanVien.SelectedRows.Item(0).Cells(6).Value

        txtmanv.Text = maNV
        txttennv.Text = tenNV
        txtngaysinhnv.Text = namSinh
        txtdiachinv.Text = diaChi
        cbbgioitinhnv.Text = gioiTinh
        txtdienthoainv.Text = dienThoai
        cbbtrinhdonv.Text = trinhDo


        'ClearTextBox()
        'dgrvNhanVien.ClearSelection()
        'Dim NhanVienList As New List(Of NhanVienGD)
        'NhanVienList = NhanVienBS.GetData()
        'cm = Me.BindingContext(NhanVienList)
        'txtmanv.DataBindings.Add("Text", NhanVienList, "MaNV")
        'txttennv.DataBindings.Add("Text", NhanVienList, "HoTen")
        'txtngaysinhnv.DataBindings.Add("Text", NhanVienList, "NamSinh")
        'txtdiachinv.DataBindings.Add("Text", NhanVienList, "DiaChi")
        'cbbgioitinhnv.DataBindings.Add("Text", NhanVienList, "GioiTinh")
        'txtdienthoainv.DataBindings.Add("Text", NhanVienList, "DienThoai")
        'cbbtrinhdonv.DataBindings.Add("Text", NhanVienList, "TrinhDo")
        'dgrvNhanVien.DataSource = NhanVienList


        'txtmanv.Text = dgrvNhanVien.CurrentRow.Cells(0).Value.ToString()
        'txttennv.Text = dgrvNhanVien.SelectedRows(0).Cells(1).Value.ToString()
        'txtngaysinhnv.Value = Date.Parse(dgrvNhanVien.SelectedRows(0).Cells(2).Value)
        'txtdiachinv.Text = dgrvNhanVien.SelectedRows(0).Cells(3).Value.ToString()
        'cbbgioitinhnv.Text = dgrvNhanVien.SelectedRows(0).Cells(4).Value.ToString()
        'txtdienthoainv.Text = dgrvNhanVien.SelectedRows(0).Cells(5).Value.ToString()
        'cbbtrinhdonv.Text = dgrvNhanVien.SelectedRows(0).Cells(6).Value.ToString()


    End Sub

    Private Sub btnTimNV_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimNV.Click
        dgrvNhanVien.DataSource = NhanVienBS.GetByName(txtTimNV.Text)
    End Sub

    '-------- Các Sự Kiện


    Private Sub txtTimNV_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles txtTimNV.MouseClick
        radiotimnv.Checked = True
    End Sub

    Private Sub radiotimnv_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles radiotimnv.MouseClick
        txtTimNV.Focus()
    End Sub

    Private Sub RadioButton1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RadioButton1.MouseClick
        LoadDGV_NhanVien()
    End Sub


    '------------------ Danh mục Form'

    Private Sub DanhSáchKháchHàngToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DanhSáchKháchHàngToolStripMenuItem.Click
        danhsachKH.Show()
    End Sub

    Private Sub DanhSáchNhómHàngToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DanhSáchNhómHàngToolStripMenuItem.Click
        danhsachnhomhang.Show()
    End Sub

    Private Sub DanhSáchMặtHàngToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DanhSáchMặtHàngToolStripMenuItem.Click
        danhsachmathang.Show()
    End Sub

    Private Sub ThoátToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ThoátToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub DanhSáchNhàCungCấpToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DanhSáchNhàCungCấpToolStripMenuItem.Click
        danhsachnhacungcap.Show()
    End Sub

    Private Sub DanhSáchNướcSảnXuấtToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DanhSáchNướcSảnXuấtToolStripMenuItem.Click
        danhsachhangsx.Show()
    End Sub

    Private Sub BánHàngToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BánHàngToolStripMenuItem.Click
        banhang.Show()
    End Sub

    Private Sub ChứngTừXuấtHàngToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChứngTừXuấtHàngToolStripMenuItem.Click
        chungtuxuathang.Show()
    End Sub

    Private Sub ChứngTừNhậpHàngToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChứngTừNhậpHàngToolStripMenuItem.Click
        chungtunhaphang.Show()
    End Sub

    
End Class